package cc.rengu.redp.bizimpl.upmp.service.impl;

import cc.rengu.redp.bizimpl.enums.IgasYesOrNoEnum;
import cc.rengu.redp.bizimpl.upmp.domain.UpmpUserApexInfo;
import cc.rengu.redp.bizimpl.upmp.dao.UpmpUserApexInfoMapper;
import cc.rengu.redp.bizimpl.upmp.service.IUpmpUserApexInfoService;
import cc.rengu.redp.common.exception.RedpException;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.collect.Maps;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.Map;

/**
 * @author System
 */
@Service("upmpUserApexInfoService")
@DS("upmp")
public class UpmpUserApexInfoServiceImpl extends ServiceImpl<UpmpUserApexInfoMapper, UpmpUserApexInfo> implements IUpmpUserApexInfoService {
    private Logger logger = LoggerFactory.getLogger(UpmpUserApexInfoServiceImpl.class);

    @Override
    public UpmpUserApexInfo findByProperty(String instId, String userType, String propertyKey, String propertyValue) {
        LambdaQueryWrapper<UpmpUserApexInfo> queryWrapper = new LambdaQueryWrapper<>();
        if (!StringUtils.isEmpty(instId)) {
            queryWrapper.eq(UpmpUserApexInfo::getInstId, instId);
        }
        if (!StringUtils.isEmpty(userType)) {
            queryWrapper.eq(UpmpUserApexInfo::getUserType, userType);
        }
        if (!StringUtils.isEmpty(propertyKey)) {
            queryWrapper.eq(UpmpUserApexInfo::getPropertyKey, propertyKey);
        }
        if (!StringUtils.isEmpty(propertyValue) ) {
            queryWrapper.eq(UpmpUserApexInfo::getPropertyValue, propertyValue);
        }
        queryWrapper.eq(UpmpUserApexInfo::getPropertyStatus, IgasYesOrNoEnum.Yes.getType());
        List<UpmpUserApexInfo> userApexInfos = this.baseMapper.selectList(queryWrapper);
        if(ObjectUtils.isEmpty(userApexInfos)){
            return userApexInfos.get(0);
        }
        return null;
    }

    @Override
    public List<UpmpUserApexInfo> findPropertyByUserInfo(String instId, String userId)throws RedpException {
        LambdaQueryWrapper<UpmpUserApexInfo> queryWrapper = new LambdaQueryWrapper<>();
        if (!StringUtils.isEmpty(instId)&&!StringUtils.isEmpty(userId)) {
            queryWrapper.eq(UpmpUserApexInfo::getInstId, instId).eq(UpmpUserApexInfo::getUserId, userId);
        }else {
            logger.error("查询用户的附加信息参数缺失[instId:{},userId:{}]，返回null",instId,userId);
            return null;
        }

        List<UpmpUserApexInfo> userApexInfos = this.baseMapper.selectList(queryWrapper);
        return userApexInfos;
    }

    @Override
    public Map<String, String> findPropertyMapByUserInfo(String instId, String userId) throws RedpException {
        List<UpmpUserApexInfo> userApexInfos = this.findPropertyByUserInfo(instId,userId);
        Map<String, String> result = Maps.newHashMap();
        for(UpmpUserApexInfo apex:userApexInfos){
            result.put(apex.getPropertyKey(),apex.getPropertyValue());
        }
        return result;
    }
}
